package demo.day11_CollectionAddpackage;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/8/1-08-01-15:10
 * @Description：demo.day11_CollectionAddpackage
 */
public class collection_use {
    public static void main(String[] args) {
//        //ArrayList实现了List接口，List实现了Collection接口
//        Collection collection = new ArrayList();
//        collection.add(1);
//        collection.add("abc");
//        collection.add(2);
//        System.out.println(collection);
//
//        String[] s = {"a","b","c"};
//        collection.addAll(Arrays.asList(s));
//        System.out.println(collection);
//
//        System.out.println(collection.contains("a"));    //true
//
//        collection.remove("abc");
//        System.out.println(collection);
//
//        collection.retainAll(Arrays.asList(s));      //将和s集合中不同的元素都删除掉
//        System.out.println(collection);
//
//        System.out.println(collection.toArray());
//        /*
//        collection中没有根据下标获取元素的方法，     比如Set接口是没有索引的
//         */
//
//        //迭代器
//        Iterator iterator = collection.iterator();
//        while (iterator.hasNext()){
//            System.out.print(iterator.next() + " ");
//        }
        ArrayList<Integer> list = new ArrayList<>();
        list.addAll(Arrays.asList(23,1,44,53,2,78));
        System.out.println(MAX(list));
    }
    public static <T extends Comparable<T>> T MAX(Collection<T> list){
        T max ;
        if(list.size() == 0){
            throw new NullPointerException("集合为空");
        }
        Iterator<T> iterator = list.iterator();
        max =  iterator.next();
        while(iterator.hasNext()){
            T next = iterator.next();
            if(next.compareTo(max) > 0){
                max = next;
            }
        }
        return max;
    }
}
